import {ajax} from "../../utils/httpRequest.js";
import {URI_GETORDERLIST, URI_ORDERPAY, URI_CANCELORDER} from "../../api.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    status: 0,
    order_list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      status: options.status
    })
    this.getOrderList();
  },

  getOrderList() {
    ajax.get(URI_GETORDERLIST, {
      status: this.data.status,
    }, {}, res => {
      this.setData({
        order_list: res.data.data.data
      });
    });
  },
  //订单支付
  orderPay(e) {
    let orderId = e.currentTarget.dataset.id;
    let createTime = e.currentTarget.dataset.createtime;
    let goodsId = e.currentTarget.dataset.goodsid;
    let goodsNum = e.currentTarget.dataset.goodsnum;
    console.log(goodsId, goodsNum);
    let nowTimeDesc30 = this.nowTimeDesc30();
    if (nowTimeDesc30 > createTime || nowTimeDesc30 == createTime) {
      //判断订单超过半小时就是过期
      wx.showModal({
        title: "提示",
        content: "订单已过期；请重新下单",
        success: function (res) {
          if (res.confirm) {
            wx.redirectTo({
              url: `/pages/order-submit/order-submit?skuId=${goodsId}&num=${goodsNum}`
            });
          }
        }
      });
      return;
    }
    this.fucOrderPay(orderId)
  },
  nowTimeDesc30() {
    let oDate = new Date();
    let y = oDate.getFullYear();
    let m = oDate.getMonth() + 1;
    m = m < 10 ? '0' + m : m;
    let d = oDate.getDate();
    d = d < 10 ? '0' + d : d;

    let h = oDate.getHours();
    h = h < 10 ? '0' + h : h;
    let mm = oDate.getMinutes() - 30;
    mm = mm < 10 ? '0' + mm : mm;
    let s = oDate.getSeconds();
    s = s < 10 ? '0' + s : s;
    return `${y}-${m}-${d} ${h}:${mm}:${s}`;
  },
  fucOrderPay(id) {
    ajax.post(URI_ORDERPAY, {
      orderId: id
    }, {}, res => {
      wx.requestPayment({
        'timeStamp': res.data.data.timeStamp,
        'nonceStr': res.data.data.nonceStr,
        'package': res.data.data.packages,
        'signType': res.data.data.signType,
        'paySign': res.data.data.paySign,
        success: function (e) {
          wx.redirectTo({
            url: "/pages/order/order?status=1",//待发货
          });
        },
        fail: function (e) {
          console.log(e);
          //支付失败转到待支付订单待付款列表
          if (e.errMsg == "requestPayment:fail" || e.errMsg == "requestPayment:fail cancel") {
            wx.showModal({
              title: "提示",
              content: "订单尚未支付",
              showCancel: false,
              confirmText: "确认",
              success: function (res) {
                if (res.confirm) {
                  wx.redirectTo({
                    url: "/pages/order/order?status=0",
                  });
                }
              }
            });
          }
        }
      });
    })
  },
  //取消订单
  cancelOrder(e) {
    wx.showModal({
      title: '温馨提示',
      content: "是否取消该订单？",
      cancelText: '否',
      confirmText: '是',
      success: (res) => {
        if (res.confirm) {
          ajax.get(URI_CANCELORDER, {
            orderId: e.currentTarget.dataset.id
          }, {}, res => {
            wx.showModal({
              title: '提示',
              content: "订单取消成功！",
              success: (res) => {
                if (res.confirm) {
                  this.getOrderList();
                }
              }
            })
          });
        }
      }
    })

  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  onShareAppMessage: function (res) {
    if (res.form === 'button') {
      //来自页内转发
      console.log(res.target);
    }
    return {
      title: '择天惠-一个做优惠的小程序',
      path: 'pages/fenlei/fenlei',
      success: function (res) {
        console.log('转发成功');
      },
      fail: function (err) {
        console.log('转发失败')
      }
    }
  }
})